#ifndef MY_PARAM_H
#define MY_PARAM_H

#include <QDebug>

// 移动方向，默认right
enum MOVE_DIRECTION {
    MOVE_DIRECTION_UP=1, MOVE_DIRECTION_DOWN=2, MOVE_DIRECTION_LEFT=3, MOVE_DIRECTION_RIGHT=4
};

// 游戏运行状态
enum GAME_STATUS {
    GAME_STATUS_INIT=0, GAME_STATUS_RUNNING=1, GAME_STATUS_PAUSE=2, GAME_STATUS_FINISH=3
};

// snake移动速度。对应定时器的超时次数
enum GAME_SPEED {
    GAME_SPEED_SLOW = 50, GAME_SPEED_EASY = 40, GAME_SPEED_NORMAL = 30, GAME_SPEED_MEDIUM = 20, GAME_SPEED_HARD = 10,
};

// snake节点
class snakeNode {

public:
    snakeNode(int x_, int y_, int row_, int col_) :x(x_), y(y_),row(row_),col(col_),next(nullptr),pre(nullptr) {}
    ~snakeNode() {
        next = nullptr;
        pre = nullptr;
        qDebug()<<"free node";
    }
public:
    // 方块左上角x坐标
    int x;
    // 方块左上角y坐标
    int y;
    // 方块所在行
    int row;
    // 方块所在列
    int col;
    snakeNode* next;
    snakeNode* pre;
};

// snake定义
class snake {

public:
    snake(): head(nullptr),tail(nullptr){}
    ~snake() {
        clear();
        head = nullptr;
        tail = nullptr;
    }

public:
    // 添加snake节点
    void addNode(snakeNode* node) {
        if(head == nullptr) {
            head = tail = node;
        }else {
            tail->next = node;
            node->pre = tail;
            tail = node;
        }
    }
    // 移动snake
    void move(snakeNode* newTail) {
        snakeNode* t = head;

        addNode(newTail);
        head = head->next;
        head->pre = nullptr;

        delete t;
    }
    // 清除snake
    void clear() {
        snakeNode* node = head;
        while (node != nullptr) {
            head = head->next;
            delete node;
            node = head;
        }
        head = nullptr;
        tail = nullptr;
    }

public:
    // snake的尾巴
    snakeNode* head;
    // snake移动的头
    snakeNode* tail;
};

// 游戏参数
class myArg {

public:
    ~myArg() {
        delete mySnake;
        delete scoreNode;
    }

public:
    // 方块行数
    int rows;
    // 方块列数
    int cols;
    // 方块大小
    int blockWidth;
    // 按键修改的方向
    int direction;
    // 刷新时的最终方向
    int safeDirection;
    // 游戏状态
    int gameStatus;
    // 定时器超时时间
    int tickerMs;
    // 定时器超时次数，控制snake的刷新速度
    int tickerCount;
    // 移动速度
    int speed;
    // 得分
    int score;
    // snake
    snake* mySnake;
    // 得分节点
    snakeNode* scoreNode;
};

#endif // MY_PARAM_H
